/**
 * Chrowety
 * bg/layout.js
 */

var layout = {};

/**
 * Retorna o tweet formatado
 * @param json {Object} Tweet
 * @param type {String} Tipo de Timeline
 * @param name {String} Nome da Timeline
 * @param isAlert {Boolean} Se for alerta
 */
layout.tweet = function(type, name, json, isAlert )
{
	if(!isObj(json))
		return console.warn('layout.tweet Error: Non Obj', json);

	var 
	Tweet 	= cloneObj(json),
	isRt 	= json.retweeted_status,
	Rt 		= isRt || json;

	Tweet['fav'] = type === 'default' && name === 'favorites' ? 
			'true' : 
				'false';	
	
	Tweet['tId']						= Rt.id;				
	Tweet['text']						= Rt.text;
	Tweet['entities']					= Rt.entities;
	Tweet['source']						= Rt.source
		.replace( utils.rgx['rel_tag'], '')
		.replace( utils.rgx['href_tag'], 'data-tweet-external="link" data-tweet-external-data="$1"');
	
	Tweet['userId'] 					= Rt.user.id;	
	Tweet['userName']					= Rt.user.name;
	Tweet['created_at']					= Rt.created_at;
	Tweet['userScreenName'] 			= Rt.user.screen_name;
	Tweet['userProfileImage'] 			= Rt.user.profile_image_url;
	
	Tweet['protectedClass']				= Rt.user['protected'] ? '' : 'dn' ;
	
	Tweet['inReplyToClass']				= Rt.in_reply_to_status_id ? '' : 'dn';
	Tweet['inReplyToScreenName'] 		= Rt.in_reply_to_screen_name || '';
	Tweet['inReplyToStatusId'] 			= Rt.in_reply_to_status_id || '';	
	
	Tweet['retweetedClass'] 			= isRt ? '' : 'dn';
	Tweet['retweetedByScreenName'] 		= isRt ? json.user.screen_name : '';
	Tweet['retweetedByStatusId'] 		= isRt ? json.id : '';
	Tweet['retweetedCreatedAt'] 		= isRt ? json.created_at : '';	
	
	Tweet['statusUrl']					= twitter.url( Rt.user.screen_name + '/status/' + Rt.id );
	Tweet['geoLocation']				= layout.geo( Rt, isAlert );
	Tweet['text'] 						= utils.text.init( Rt );

	if( isAlert )
	{
		Tweet['timeout']	  = isAlert && utils.time.s( cfg.get('alerts.timeout') );
		Tweet['created_at']	  = utils.time.tweet( json.created_at );
		Tweet['via']		  = i18n.get('via');
		Tweet['timelineName'] = name;
	}	
	
	return ( isAlert ? L_ALERT : L_TWEET ).replaceWithObject( Tweet );
};


/**
 * Retorna a DM formatada
 * @param json {Object} TweetMsg
 */
layout.dm = function( type, name, json, isAlert )
{
	if(!isObj(json))
		return console.warn('layout.dm Error: Non Obj', json);

	var Tweet = cloneObj(json);

	Tweet['fav']						= 'false';
	
	Tweet['tId']						= json.id;				
	Tweet['userId'] 					= json.sender.id;	
	Tweet['userName']					= json.sender.name;
	Tweet['created_at']					= json.created_at;
	Tweet['userScreenName'] 			= json.sender.screen_name;
	Tweet['userProfileImage'] 			= json.sender.profile_image_url;
	
	Tweet['text'] 						= utils.text.init( Tweet );

	if( isAlert )
	{
		Tweet['timeout']	  = isAlert && utils.time.s( cfg.get('alerts.timeout') );
		Tweet['created_at']	  = utils.time.tweet( json.created_at );
		Tweet['via']		  = i18n.get('via');
		Tweet['timelineName'] = name;		
	}	
	
	return ( isAlert ? L_ALERT_DM : L_DM ).replaceWithObject( Tweet );
};

/**
 * Retorna um tweet search formatado
 * @param json {Object} SearchMsg
 */
layout.search = function( type, name, json, isAlert )
{
	if(!isObj(json))
		return console.warn('layout.search Error: Non Obj', json);

	var Tweet = cloneObj(json);

	Tweet['fav']		= 'false';
	
	Tweet['source']		= json.source
		.replaceAll('&quot;', '"')
		.replaceAll('&gt;', '>')
		.replaceAll('&lt;', '<')
		.replace( utils.rgx['rel_tag'], '')
		.replace( utils.rgx['href_tag'], 'data-tweet-external="link" data-tweet-external-data="$1" class="data"');
	
	Tweet['statusUrl']	 = twitter.url( Tweet.from_user + '/status/' + Tweet.id );
	Tweet['geoLocation'] = layout.geo( json, isAlert );
	Tweet['text']		 = utils.text.init( Tweet );
	
	if( isAlert )
	{
		Tweet['timeout']	  = isAlert && utils.time.s( cfg.get('alerts.timeout') );
		Tweet['created_at']	  = utils.time.tweet( json.created_at );
		Tweet['via']		  = i18n.get('via');
		Tweet['timelineName'] = name;		
	}
	
	return ( isAlert ? L_ALERT_SEARCH : L_SEARCH ).replaceWithObject( Tweet );
};

/** 
 * Retorna o Geo de um tweet
 * @param tweet {Object}
 * @return html {String}
 */
layout.geo = function( tweet, isAlert )
{
	if( isObj(tweet) && ( tweet.geo || tweet.coordinates ) )
	{
		var coords;
		
		if( tweet.coordinates )
			coords = tweet.coordinates.coordinates;
		else if( tweet.geo )
			coords = tweet.geo ;
		
		coords = JSON.stringify( coords );
		
		if( tweet.place && !isAlert )
		{
			var 
			title = tweet.place.full_name + "\n" + tweet.place.country + ' (' + tweet.place.country_code + ')' ,
			box = '';
			
			if( tweet.place.bounding_box )
				box = JSON.stringify( tweet.place.bounding_box.coordinates );
			
			return '<a data-geolocation-coords="'+ coords +'" data-geolocation-box="'+ box +'" title="'+ title +'" class="location">'+ tweet.place.full_name +'</a>';
		}
		else
			return '<a data-geolocation-coords="'+ coords +'" data-i18n="title(show_geolocation)" class="location"></a>';
	}
	else
		return ' ';
};

var
L_TWEET = 
'<article class="TweetCon" data-tweetId="{{id}}" data-userId="{{userId}}" data-screenName="{{userScreenName}}">'+
		'<header class="header">'+
		    '<figure class="avatar" title="{{userName}} ({{userScreenName}})">'+
		        '<a data-tweet-external="mention" data-tweet-external-data="{{userScreenName}}">'+
		            '<img width="45" height="45" src="{{userProfileImage}}"/>'+
		        '</a>'+
		        '<span class="rts {{retweetedClass}}"></span>'+
		        '<figcaption class="screen_name">'+
		        	'<a class="user" data-tweet-external="mention" data-tweet-external-data="{{userScreenName}}">{{userScreenName}}</a>: '+
		        	'<a class="cadeado {{protectedClass}}"></a> '+
		        '</figcaption>'+		        
		    '</figure>'+
		    
		    '<aside class="actions">'+
		        '<span><a class="star" fav="{{fav}}"></a></span>'+
		        '<span><a class="msg"></a></span>'+
		        '<span><a class="trans"></a></span>'+
		        '<span class="retweet">'+
		        	'<span class="rt"></span>'+
		        	'<span class="retweet_method">'+
		        		'<button data-action="Tweet.Retweet">Retweet</button>'+
		        		'<button data-action="Tweet.Rt">RT</button>'+
		        	'</span>'+
		        '</span>'+
		    '</aside>'+
		    '<span class="text">{{text}}</span>'+
	    '</header>'+
	    	    	    
	    '<footer>'+
	    	'<div class="separator"></div>'+
		    '<span class="footer">'+
		    	'{{geoLocation}}'+
		        '<time class="created_at" datetime="{{created_at}}">'+
		        	'<a data-tweet-external="link" data-tweet-external-data="{{statusUrl}}" data-datetime="{{created_at}}" title="{{tId}}"></a>'+
		        '</time> '+		        
		        '<span class="via" data-i18n="via"></span> '+
		        '<span class="source">{{source}}</span> '+
		        '<span class="in_reply_to {{inReplyToClass}}">'+
		        	'<a data-id="{{inReplyToStatusId}}" data-action="tweet.conversation">'+
		        		'<span data-i18n="in_reply_to:{{inReplyToScreenName}}"></span> '+
		        	'</a>'+
		        '</span> '+
		    '</span> '+
		    
		    '<div class="retweeted {{retweetedClass}}">'+
		    	'<br> '+
		        '<span class="retweeted_by" data-i18n="retweeted_by"></span> '+
		        '<span class="screen_name">'+
		        	'<a data-tweet-external="mention" data-tweet-external-data="{{retweetedByScreenName}}" class="user">{{retweetedByScreenName}}</a>'+
		        '</span> '+
		        '<time class="created_at">'+
		        	'(<a data-tweet-external="link" data-tweet-external-data="{{statusUrl}}" title="{{retweetedByStatusId}}" data-id="{{retweetedByStatusId}}" data-datetime="{{retweetedCreatedAt}}"></a>)'+
		        '</time> '+
		    '</div>'+
		    
		    '<span class="actionsSub dn">'+
		        '<span class="retweet"><a data-i18n="retweet"></a></span>'+
		    '</span>'+
		    
		    '<span class="reply_button" data-action="Tweet.Reply" >'+
		        '<a data-i18n="reply"></a>'+
		    '</span>'+
	    '</footer> '+	    
'</article>';

var
L_DM = 
'<article class="TweetCon" data-tweetId="{{id}}" data-userId="{{userId}}" data-screenName="{{userScreenName}}">'+
		'<header class="header">'+
		    '<figure class="avatar" title="{{userName}} ({{userScreenName}})">'+
		        '<a data-tweet-external="mention" data-tweet-external-data="{{userScreenName}}">'+
		            '<img width="45" height="45" src="{{userProfileImage}}"/>'+
		        '</a>'+
		        '<figcaption class="screen_name">'+
		        	'<a class="user" data-tweet-external="mention" data-tweet-external-data="{{userScreenName}}">{{userScreenName}}</a>: '+
		        '</figcaption>'+
		    '</figure>'+
		    
		    '<aside class="actions">'+
		        '<span class="translate"><a class="trans"></a></span>'+
		    '</aside>'+
		    '<span class="text">{{text}}</span>'+
	    '</header>'+
	    	    	    
	    '<footer>'+
	    	'<div class="separator"></div>'+
		    '<span class="footer">'+
		        '<time class="created_at" datetime="{{created_at}}" data-datetime="{{created_at}}"></time> '+
		    '</span> '+
		    
		    '<span class="reply_button" data-action="Tweet.ReplyDM" >'+
		        '<a data-i18n="reply"></a>'+
		    '</span>'+
	    '</footer> '+
'</article>';

var
L_SEARCH = 
'<article class="TweetCon" data-tweetId="{{id}}" data-userId="{{from_user_id}}" data-screenName="{{from_user}}">'+
		'<header class="header">'+
		    '<figure class="avatar" title="{{from_user}}">'+
		        '<a data-tweet-external="mention" data-tweet-external-data="{{from_user}}">'+
		            '<img width="45" height="45" src="{{profile_image_url}}"/>'+
		        '</a>'+
		        '<figcaption class="screen_name">'+
		        	'<a class="user" data-tweet-external="mention" data-tweet-external-data="{{from_user}}">{{from_user}}</a>: '+
		        '</figcaption>'+
		    '</figure>'+
		    
		    '<aside class="actions">'+
		    	'<span class="favorite"><a class="star" fav="{{fav}}"></a></span>'+
		        '<span class="message"><a class="msg"></a></span>'+
		        '<span class="translate"><a class="trans"></a></span>'+
		        '<span class="retweet">'+
	        	'<span class="rt"></span>'+
	        	'<span class="retweet_method">'+
	        		'<button data-action="Tweet.Retweet">Retweet</button>'+
	        		'<button data-action="Tweet.Rt">RT</button>'+
	        	'</span>'+
	        '</span>'+		        
		    '</aside>'+
		    '<span class="text">{{text}}</span>'+
	    '</header>'+
	    	    	    
	    '<footer>'+
	    	'<div class="separator"></div>'+
		    '<span class="footer">'+
		        '<time class="created_at" datetime="{{created_at}}">'+
		        	'<a data-tweet-external="link" data-tweet-external-data="{{statusUrl}}" title="{{id}}" data-datetime="{{created_at}}"></a>'+
		        '</time> '+
		        '<span class="via" data-i18n="via"></span> '+
		        '<span class="source">{{source}}</span> '+		        
		    '</span> '+
		    
		    '<span class="reply_button" data-action="Tweet.Reply" >'+
		        '<a data-i18n="reply"></a>'+
		    '</span>'+
	    '</footer> '+
'</article>';

var 
L_ALERT = 
'<scr'+'ipt>Timeout = {{timeout}};</scr'+'ipt>'+
'<article class="tweet">'+
    '<a title="{{userName}} ({{userScreenName}})" href="{{profileUrl}}" target="_blank" class="nome">'+
        '<img class="foto fl" src="{{userProfileImage}}" width="45" height="45" />'+
        '<span>{{userName}}</span>'+
    '</a>'+
    '<div class="texto">{{text}}</div>'+
    '<div class="cb">'+
        '<span class="fl data">'+
            '{{geoLocation}}'+
            '{{created_at}}'+
            '<span> {{via}} </span>'+
            '<a class="data">{{source}}</a>'+
        '</span>'+
        '<span class="data fr">{{timelineName}}</span>'+
    '</div>'+
    '<br />'+
'</article>';

var 
L_ALERT_DM = 
'<scr'+'ipt>Timeout = {{timeout}};</scr'+'ipt>'+
'<article class="tweet">'+
    '<a title="{{userName}} ({{userScreenName}})" href="{{profileUrl}}" target="_blank" class="nome">'+
        '<img class="foto fl" src="{{userProfileImage}}" width="45" height="45" />'+
        '<span>{{userName}}</span>'+
    '</a>'+
    '<div class="texto">{{text}}</div>'+
    '<div class="cb">'+
        '<span class="fl data">'+
            '{{geoLocation}}'+
            '{{created_at}}'+
            '<span> {{via}} </span>'+
            '<a class="data">{{source}}</a>'+
        '</span>'+
        '<span class="data fr">{{timelineName}}</span>'+
    '</div>'+
    '<br />'+
'</article>';

var 
L_ALERT_SEARCH = 
'<scr'+'ipt>Timeout = {{timeout}};</scr'+'ipt>'+
'<article class="tweet">'+
    '<a title="{{from_user}}" href="{{profileUrl}}" target="_blank" class="nome">'+
        '<img class="foto fl" src="{{profile_image_url}}" width="45" height="45" />'+
        '<span>{{from_user}}</span>'+
    '</a>'+
    '<div class="texto">{{text}}</div>'+
    '<div class="cb">'+
        '<span class="fl data">'+
            '{{geoLocation}}'+
            '{{created_at}}'+
            '<span> {{via}} </span>'+
            '<a class="data">{{source}}</a>'+
        '</span>'+
        '<span class="data fr">{{timelineName}}</span>'+
    '</div>'+
    '<br />'+
'</article>';

var 
L_REPLY = 
'<article class="TweetConReplyRT" id="{{id}}" data-tid="{{tid}}">'+
	'<span class="urlShortener">'+
		'<input data-focus="urlShortenerFocus" type="text" data-i18n="placeholder(url_shortener)" spellcheck="false">'+
		'<button data-i18n="ok"></button>'+
	'</span>'+
	'<textarea id="replyText_{{tid}}"></textarea>'+
	'<span id="replyCounter_{{tid}}" class="counter">140</span>'+        			
	'<span class="anexado">'+
		'<button data-i18n="title(special_chars)">&curren;</button>'+
		'<button data-i18n="title(hash_tags)">#</button>'+
	'</span>'+		
	'<span id="replyStatus_{{tid}}" class="status"></span>'+
	'<div class="cb"></div>'+	
	'<button id="replySend_{{tid}}" data-i18n="done" data-action="Tweet.SendReply" disabled="disabled"></button>'+
	'<a data-i18n="cancel" data-action="Tweet.CancelReply"></a>'+
'</article>';